package com.example.userservice.services;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class T1 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        List<String> data = new ArrayList<>();

        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(()->{
            System.err.println(Thread.currentThread().getName());

            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

        }, Executors.newCachedThreadPool());

        CompletableFuture<Void> runAsync2 = CompletableFuture.runAsync(()->{
            data.add("1");
            System.out.println("1:"+ Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }},Executors.newCachedThreadPool());

        CompletableFuture<Void> runAsync3 = CompletableFuture.runAsync(()->{
            data.add("2");
            System.out.println("2:"+ Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }},Executors.newCachedThreadPool());

        CompletableFuture.allOf(runAsync,runAsync2,runAsync3)

                .whenCompleteAsync((v,e) ->{
                    System.err.print("---");
                    System.err.print(v);
                    System.err.print(e);
                    System.err.println(data);
                    System.err.println("结束");
                    System.err.println("总耗时：" + (System.currentTimeMillis() - startTime)/1000);
                }
                  //.whenComplete()
                  //.whenComplete();

                );




        System.err.println("外部总耗时：" + (System.currentTimeMillis() - startTime)/1000);

    }
}
